#include <string>
#include <iostream>

#include "test.h"
#include "print.hpp"

using std::cin;
using std::cout;
using std::endl;
using std::string;

long get_a_target_long() {
    long target{0};

    cout << "target (0~" << RAND_MAX <<  "): ";
    cin >> target;

    return target;
}

int compareLongs(const void*a, const void* b) {
    return (*(long*)a) - (*(long*)b);
}

string get_a_target_string() {
    long target{0};
    char buf[10];

    cout << "target (0~" << RAND_MAX <<  "): ";
    cin >> target;
    snprintf(buf, 10, "%ld", target); // long integer

    return string(buf);
}

int compareStrings(const void*a, const void* b) {
    if ((*(string*)a) > *(string*)b) {
        return 1;
    } else if ((*(string*)a) < *(string*)b) {
        return -1;
    } else {
        return 0;
    }
}

double transformClockSeconds(const clock_t& millisec) {
    return double(millisec) / CLOCKS_PER_SEC;
}

string get_a_random_string(const long& size) {
    auto buf = new char[size];
    string str;
    try {
        snprintf(buf, size, "%d", rand());
        str = buf;
    } catch(std::exception& err) {
        std::cerr << err.what() << endl;
        abort();
    }

    delete [] buf;
    return str;
}

// TimeCounter
void TimeCounter::start() {
    timeStart = clock();
}

void TimeCounter::end() {
    timeEnd = clock();

    seconds = transformClockSeconds(timeEnd - timeStart);
}

void TimeCounter::show(const std::string &message) {
    print(message, seconds, "s");
}

void TimeCounter::endAndShow(const std::string &message) {
    end();
    show(message);
}

void TimeCounter::endAndShow() {
    end();
    show("usage time: ");
}

// 延时函数
void TimeCounter::wait(const long& value) {
    timeStart = clock();
    while (clock() - value < timeStart) {

    }
}

// 辅助测试函数完成
// copy 到测试程序处，或者引用
